<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%!String name = null;
	Connection conn;
	PreparedStatement pstmt;
	Statement stmt;%>
	<%!public Connection conn() {
		try {
			Class.forName("com.mysql.jdbc.Driver");// 加载Mysql数据驱动
			conn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/test", "root", "198666");// 创建数据连接
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				conn.close();
			} catch (SQLException s) {
				s.printStackTrace();
			}
		}
		return conn;
	}%>
	<%!public boolean enquire(String empno) {
		conn = conn();
		String enquire = "select ename from emp where empno = '" + empno + "'";
		boolean flag = false;
		try {
			stmt = conn.createStatement();
			ResultSet enquireRs = stmt.executeQuery(enquire);
			while (enquireRs.next()) {
				flag = true;
			}
			enquireRs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}%>
	<%!public boolean insert(String empno, String ename, String job,
			String hiredate, String sal, String comm) {
		conn = conn();
		boolean flag = false;
		String insert = "insert into emp(empno,ename,job,hiredate,sal,comm) values('"
				+ empno
				+ "','"
				+ ename
				+ "','"
				+ job
				+ "','"
				+ hiredate
				+ "','" + sal + "','" + comm + "');";
		try {
			stmt = conn.createStatement();
			if (1 == stmt.executeUpdate(insert)) {
				flag = true;
			}
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}%>
	<%
		String empno = request.getParameter("empno");
		String ename = request.getParameter("ename");
		String job = request.getParameter("job");
		String hiredate = request.getParameter("hiredate");
		String sal = request.getParameter("sal");
		String comm = request.getParameter("comm");
		if (!("".equals(empno) || empno == null || "".equals(ename)
				|| ename == null || "".equals(job) || job == null
				|| "".equals(hiredate) || hiredate == null
				|| "".equals(sal) || sal == null || "".equals(comm) || comm == null)) {
			boolean enquireResult = enquire(empno);
			if (enquireResult) {
				response.setHeader("refresh", "2;url=create.jsp");
	%>
	<h2>
		<%=empno%>已存在，创建失败，2秒后跳转到创建页，如果没有跳转，或者点击<a href="create.jsp">这里</a>跳转。
	</h2>
	<%
		} else {
				if (insert(empno, ename, job, hiredate, sal, comm)) {
	%>
	<h2>
		插入成功,点击<a href="create.jsp">这里</a>返回创建页
	</h2>
	<%
		} else {
	%>
	<h2>插入失败</h2>
	<%
		}
			}

		} else {
	%>
	<h2>有空值。</h2>
	<%
		}
	%>
</body>
</html>